#include <iostream>
#include <cmath>
#include <iomanip>


using namespace std;

double buy(double, double, double, double);
double lease(double, double, double, double);
double calcPayment(double, double, double);


void AutoBuyOrLease()
{
	
	double pp; //purchase price
	double dp; //down payment
	double la; //loam amount
	double st; //sales tax
    double tax; //tax
    double sub; //subtotal
	double netB; //net buy cost
	double netL; //net lease cost

	
	cout << "Welcome to the Buy or Lease a Car Calculator" << endl << endl;
	cout << "Programmed by Timothy Schubert" << endl << endl;

	cout << "First we have to get the basics" << endl << endl;

	cout << "Please enter the following:" << endl << endl;

	cout << "Purchase Price: $";
	cin >> pp;
	cout << endl;

	cout << "Down Payment: $";
	cin >> dp;
	cout << endl;

	cout << "Sales Tax Rate (%) : ";
	cin >> st;
	st = (st/100);
	cout << endl;

    tax = (pp*st);
    la = ((pp-dp)-tax);

	cout << "Now We'll Calcuate for Financing" << endl << endl;

	netB = buy(pp, dp, st, la);

	cout << "Now We'll Calculate for Leasing" << endl << endl;

	netL = lease(pp, dp, st, la);


	system("PAUSE");
}

double buy(double pp,double dp, double st, double la)
{

	double lt; //loan term
	double ir; //interest rate
	double of; //other fees
	double ad; //annual depreciation
	double net; //net cost

	cout << "Please enter the following" << endl << endl;

	cout << "Loan Term (in years) : ";
	cin >> lt;
	cout << endl;

	cout << "Interest Rate (%) : ";
	cin >> ir;
	ir = (ir/100);
	cout << endl;

	cout << "Other Fees : $";
	cin >> of;
	cout << endl;

	cout << "Annual Depreciation (%) : ";
	cin >> ad;
	ad = (ad/100);
	cout << endl << endl;

	cout << "The Total Amount Spent : $";
	cout << ((calcPayment(la, ir, lt)*lt)+dp) << endl << endl;
	
	cout << "Your Yearly Payments Would Be : $";
	cout << fixed << setprecision(2) << calcPayment(la, ir, lt) << endl << endl;

	cout << "Your Monthly Payments Would Be : $";
	cout << (calcPayment(la, ir, lt)/12) << endl << endl;

	cout << "You Would Have Paid $" << ((calcPayment(la, ir, lt)*lt) - la);
	cout << " In Interest" << endl << endl;

	cout << "The Market Value of Your Vehicle "<< endl;
	cout << "After " << fixed << setprecision(0) << lt << " Years" << " Would Be: $";

	cout << fixed << setprecision(2) << (pow(1-ad,lt)*pp) << endl << endl;


	net = ((calcPayment(la, ir, lt)*lt)+dp)-(pow(1-ad,lt)*pp);

	cout << "The Net Cost of Buying is : $" << net << endl << endl;

	return net;
}

double lease(double pp,double dp, double st, double la)
{
    double lt; //loan term
	double ir; //interest rate
	double of; //other fees
    double rp; //residual percent
    double ra; //residual amount
    double mf; //money factor
    double df; //depreciation fee
    double ff; //finance fee
    double mp; //monthly payment
    double net; //net cost


    cout << "Please Enter The Following" << endl << endl;

    cout << "Lease Term (in years) : " ;
    cin >> lt;
    lt = lt*12;
    cout << endl;
       
    cout << "Interest Rate (%) : ";
	cin >> ir;
	ir = (ir/100);
	cout << endl;

	cout << "Other Fees : $";
	cin >> of;
	cout << endl;

    cout << "Residual Percent (%) : ";
    cin >> rp;
    rp = (rp/100);
    cout << endl;

    ra = rp*pp;

    mf = ir/2400;

    df = ((la+of)-ra)/lt;

    ff = ((la+of)+ra)*mf;

    mp = df+ff;

    cout << "The Total Amount Spent : $";
	cout << fixed << setprecision(2)<<(mp*lt) << endl << endl;
	
	cout << "Your Yearly Payments Would Be : $";
	cout << fixed << setprecision(2) << (((mp*lt)/lt)*12) << endl << endl;

	cout << "Your Monthly Payments Would Be : $";
	cout << mp << endl << endl;


	return 0;
}

double calcPayment(double la, double ir, double lt)
{

	double yp;
	
	yp=la/((1-(1/pow(1+ir,lt)))/ir);

	return yp;
}